package cn.fl.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysRegionApiService;
import cmm.comm.facade.framework.condition.SysStdRegionQueryCondition;
import cmm.comm.facade.framework.dto.SysStdRegionDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.service.ISysStdRegionService;
import cn.fl.system.transform.SysStdRegionVOTransform;
import cn.fl.system.vo.SysStdRegionVO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @Description: 标准地区表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class SysStdRegionServiceImpl implements ISysStdRegionService {

	/**
	 * 日志
	 */
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(ISysStdRegionService.class);
	
	/**
	 * 区域API服务
	 */
	@Autowired
	private ISysRegionApiService sysRegionApiService;

	@Override
	public JSONArray searchProvinceList() throws BaseException {
		String sysCd = CurrentThreadContext.getCurrentSysCd();
		JSONArray result = new JSONArray();
		List<SysStdRegionDTO> list = this.sysRegionApiService.searchStdProvinceList(sysCd);
		setRegionListToKV(result, list);
		return result;
	}

	@Override
	public JSONArray searchRegionListBy(String rgnPid) throws BaseException {
		JSONArray result = new JSONArray();
		String sysCd = CurrentThreadContext.getCurrentSysCd();
		List<SysStdRegionDTO> list = this.sysRegionApiService.searchStdRegionBy(Long.valueOf(rgnPid), sysCd);
		setRegionListToKV(result, list);
		return result;
	}

	/**
	 * 设置数据到KeyValueVO集合
	 * @param kvList 要返回的集合
	 * @param list DB返回的数据
	 */
	private void setRegionListToKV(JSONArray result, List<SysStdRegionDTO> list) {
		if(CollectionUtils.isNotEmpty(list)){
			JSONObject keyValueObj = null;
			for (SysStdRegionDTO sysStdRegionDTO : list) {
				keyValueObj = new JSONObject();
				keyValueObj.put("key", String.valueOf(sysStdRegionDTO.getRgnCd()));
				keyValueObj.put("value", sysStdRegionDTO.getRgnNm());
				keyValueObj.put("rgnId", sysStdRegionDTO.getId());
				result.add(keyValueObj);
			}
		}
	}

	@Override
	public List<SysStdRegionVO> searchStdRegionList(SysStdRegionQueryCondition condition) throws BaseException {
		if(null == condition)return new ArrayList<>();
		List<SysStdRegionDTO> dtoList = this.sysRegionApiService.searchStdRegionList(condition);
		return SysStdRegionVOTransform.toVOList(dtoList);
	}
	/*查询标准省级代码
	 * (non-Javadoc)
	 * @see cn.fl.system.service.ISysStdRegionService#searchStdProvinceList()
	 */
	@Override
	public List<SysStdRegionDTO> searchStdProvinceList() throws BaseException {
		String sysCd = CurrentThreadContext.getCurrentSysCd();
		List<SysStdRegionDTO> list = this.sysRegionApiService.searchStdProvinceList(sysCd);
		return list;
	}
	/*查询标准市/县级代码
	 * (non-Javadoc)
	 * @see cn.fl.system.service.ISysStdRegionService#searchStdProvinceList()
	 */
	@Override
	public List<SysStdRegionDTO> selectRegionListBy(String rgnPid) throws BaseException {
		String sysCd = CurrentThreadContext.getCurrentSysCd();
		List<SysStdRegionDTO> list = this.sysRegionApiService.searchStdRegionBy(Long.valueOf(rgnPid), sysCd);
		return list;
	}

	
}
